<?php

namespace App\Http\Models;

use App\Libs\Bos\BosClientProxy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;

/**
 * Class BosModel
 *
 * @mixin Builder
 * @mixin \Illuminate\Database\Query\Builder
 *
 * @package App\Http\Models
 */
class BosModel extends Model
{
    protected $bucket = 'panx-website';

    protected $bosKeyPrefix = 'dir-unknown/';

    public function uploadImage( $uploadFile ) 
    {
      $timeStamp = '_'.date('Y_m_d');
      $imgOriginName = $uploadFile ->getClientOriginalName();
      $imgNameArr =  explode(".",$imgOriginName);
      $imgType = $imgNameArr[1];
      $bosKey = $this->bosKeyPrefix.Hash::make($imgNameArr[0]).$timeStamp.'.'.$imgType;

      $bosClient = new BosClientProxy();
      $bosClient->client->putObjectFromFile($this->bucket, $bosKey , $uploadFile->getRealPath());

      return $bosKey;
    }

    public function uploadImages( $uploadFile , $bosKeyPrefix)
    {
        $timeStamp = '_'.date('Y_m_d');
        $imgOriginName = $uploadFile ->getClientOriginalName();
        $imgNameArr =  explode(".",$imgOriginName);
        $imgType = $imgNameArr[1];
        $bosKey = $bosKeyPrefix.time().$timeStamp.'.'.$imgType;

        $bosClient = new BosClientProxy();
        $bosClient->client->putObjectFromFile($this->bucket, $bosKey , $uploadFile->getRealPath());

        return $bosKey;
    }


    


    public function uploadLocalFile( $file_path , $bos_key )
    {
        $bosClient = new BosClientProxy();

        $bosClient->client->putObjectFromFile($this->bucket, $bos_key , $file_path );

        return $bos_key;
    }
  
    public function getUrl( $bosKey )
    {
      if (is_int(strpos($bosKey,'http'))) return $bosKey;
  
      $bosClient = new BosClientProxy();
      $imgURL = $bosClient->client->generatePreSignedUrl($this->bucket, $bosKey, $bosClient->signArray);

      return $imgURL;
    }

    public function getStaticUrl( $bosKey )
    {
        return Arr::first(explode('?', $this->getUrl($bosKey)));
    }


    public function deleteBosColumns( $column_names )
    {
        if ( ! is_array($column_names) ) {
            throw new \Exception('column_names need to be an array');
        }

        array_map( [ $this , 'deleteBosColumn' ] , $column_names );
    }

    public function deleteBosColumn( $column_name )
    {
        if ( ! $column_name || ! is_string( $column_name ) ) {
            throw new \Exception('column_name need to be an column name');
        }

        if ( ! $this->attributes[$column_name] ) return null;
        return $this->deleteBosKey($this->attributes[$column_name]);
    }

    public function deleteBosJsonColumns( $column_names )
    {
        if ( ! is_array($column_names) ) {
            throw new \Exception('column_names need to be an array');
        }

        array_map( [ $this , 'deleteBosJsonArr' ] , $column_names );
    }


    public function deleteBosKey($key)
    {
//        $bosClient = new BosClientProxy();
//        return $bosClient->client->deleteObject($this->bucket, $key);
        $bosClient = new BosClientProxy();
        try {
            $bosClient->client->deleteObject($this->bucket, $key);
        } catch (\Exception $e) {
            return 'no key or something';
        }
        return true;
    }



}
